/*
 * =====================================================================================
 *
 *       Filename:  cl_active_object.cxx
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  12/24/2010 02:28:18 AM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Ken Chen (Ken), chen_kenz@emc.com
 *        Company:  EMC
 *
 * =====================================================================================
 */

#include "cl_active_object.hxx"
#include "cl_callback.hxx"

namespace neo 
{
using namespace std;

// Active Object 
CLActiveObject::CLActiveObject() 
  : m_done(false) 
{  
  m_thr = unique_ptr<CLThread>(new CLThread(bind(&CLActiveObject::run, this)));
  m_thr->start();
}

CLActiveObject::~CLActiveObject()
{
  send( bind(&CLActiveObject::setDone, this) );
  m_thr->join();
}

void CLActiveObject::send(CLActiveObject::Callback msg)
{
  m_msgQueue.push(msg);
}

void CLActiveObject::run(void)
{
  while(!m_done)
  {
		Callback msg;
    if(m_msgQueue.pop(msg))
    {
      msg();
    }
  }
}

void CLActiveObject::setDone(void)
{
	m_done = true;
}

} // neo
